package org.linlinjava.litemall.db.service.rfid;

import cn.hutool.core.util.ObjectUtil;
import org.linlinjava.litemall.db.dao.AgedmallLgRfidGoodsCurStatMapper;
import org.linlinjava.litemall.db.domain.AgedmallLgRfidGoodsCurStat;
import org.linlinjava.litemall.db.domain.AgedmallLgRfidGoodsCurStatExample;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import java.time.LocalDateTime;

/**
 * @author wj
 * @date 2021/11/8 10:25
 */
@Service
public class AgedmallLgRfidStateService {

    @Resource
    private AgedmallLgRfidGoodsCurStatMapper agedmallLgRfidGoodsCurStatMapper;


    public void addLgRfidGoods(Long storeId,String rfid,Integer type){
        AgedmallLgRfidGoodsCurStatExample curStatExample=new AgedmallLgRfidGoodsCurStatExample();
        curStatExample.or().andRfidEqualTo(rfid).andTypeEqualTo(type);
        AgedmallLgRfidGoodsCurStat curStat = agedmallLgRfidGoodsCurStatMapper.selectOneByExample(curStatExample);
        if(ObjectUtil.isNull(curStat)){
            curStat=new AgedmallLgRfidGoodsCurStat();
            curStat.setStoreId(storeId);
            curStat.setRfid(rfid);
            curStat.setCurStat(0);
            curStat.setType(type);
            curStat.setCreatedTime(LocalDateTime.now());
            agedmallLgRfidGoodsCurStatMapper.insertSelective(curStat);
        }
        else{
            curStat.setStoreId(storeId);
            curStat.setRfid(rfid);
            curStat.setUpdateTime(LocalDateTime.now());
            agedmallLgRfidGoodsCurStatMapper.updateByPrimaryKey(curStat);
        }

    }

    public boolean changeLgRfidState(String rfid,Integer state,Integer type){
        AgedmallLgRfidGoodsCurStatExample curStatExample=new AgedmallLgRfidGoodsCurStatExample();
        curStatExample.or().andRfidEqualTo(rfid).andTypeEqualTo(type);
        AgedmallLgRfidGoodsCurStat curStat = agedmallLgRfidGoodsCurStatMapper.selectOneByExample(curStatExample);
        if(ObjectUtil.isNull(curStat)){
            return false;
        }
        else{
            curStat.setCurStat(state);
            curStat.setUpdateTime(LocalDateTime.now());
            agedmallLgRfidGoodsCurStatMapper.updateByPrimaryKeySelective(curStat);
            return true;
        }
    }
}
